//
//  TwentyOneViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//
/**✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨*/
/*使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。
 用于展示集合视图，布局更加灵活，可实现多列布局
 UICollectionViewDelegate
 
 UICollectionViewDelegate - 负责用户的交互、Cell 的外形，委托方法和 TableView 相似，可以选择性实现以下委托方法。
 
 collectionView:shouldHighlightItemAtIndexPath：是否支持高亮？
 
 collectionView:didHighlightItemAtIndexPath：如果支持高亮，那么高亮；
 
 collectionView:shouldSelectItemAtIndexPath：询问是否可以被选中？
 
 collectionView:didUnhighlightItemAtIndexPath：如果支持高亮，那么现在取消高亮；
 
 collectionView:didSelectItemAtIndexPath：如果支持可以被选中，那么选中 cell
 */




#import "TwentyOneViewController.h"
#import "AQCollectionViewLayout.h"
#import "AQCollectionViewCell.h"
@interface TwentyOneViewController ()<UICollectionViewDataSource>
@property (nonatomic ,strong)UICollectionView *collectionView;
@property (nonatomic ,strong)NSMutableArray *imageArray;
@end

@implementation TwentyOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"如果换成2列是有错误的,并没有找到答案";
    [self initUI];
    [self initUIDadaSource];
}


-(void)initUIDadaSource{
    _imageArray = [NSMutableArray array];
    for (int i = 1; i <= 20; i ++) {
        NSString *imagesName = [NSString stringWithFormat:@"%d.jpg",i];
        NSString *stringPath = [[NSBundle mainBundle]pathForResource:imagesName ofType:nil];
        UIImage *images = [UIImage imageWithContentsOfFile:stringPath];
        [_imageArray addObject:images];
    }

}


-(void)initUI{

    [self.view addSubview:self.collectionView];
}


#pragma mark - UICollectionViewDataSource
/**定义展示的UICollectionViewCell的个数  */
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _imageArray.count;
}
/**每个UICollectionView展示的内容 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    AQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.imageView.image = _imageArray[indexPath.row];
    return cell;

}




//定义展示的Section的个数
//-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 1;
//}


#pragma mark --UICollectionViewDelegate

//UICollectionView被选中时调用的方法
//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.backgroundColor = [UIColor whiteColor];
//}

//返回这个UICollectionView是否可以被选择
//-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}



#pragma mark - get
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        AQCollectionViewLayout *aqLayout = [[AQCollectionViewLayout alloc]init ];
        aqLayout.column = 3;
        __weak typeof (self) weakSelf = self;  /**弱引用*/
        [aqLayout compeleteItemHeightWithBlock:^CGFloat(NSIndexPath *indexPath, CGFloat itemWidth) {
            UIImage *image = weakSelf.imageArray[indexPath.row];
            return image.size.height / image.size.width * itemWidth;
        }];
        
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:aqLayout];
        
        _collectionView.backgroundColor = [UIColor cyanColor];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"AQCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        _collectionView.dataSource = self;
    }
    return _collectionView;
}
@end
